<?php
include("includes/adminController.php");

function show_item($item_id) {
	global $db;
	$sql = "SELECT * FROM item WHERE id = '" . $item_id . "'";
	$query = $db->sql_query($sql);
	return $db->sql_fetchrow($query);
}

if(empty($_GET['id'])){
	echo "error: item id not found ";
	exit;
}
$iid = $_GET['id'];
$item = show_item($iid);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="itelement.css" rel="stylesheet" type="text/css" />
<script src="script/jquery.js"></script>
<script src="script/ajaxfileupload.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready (
	function () {
		var iid = '<? echo $iid; ?>';
		//get attached file
		$('#attachment').html('<font size="3">loading</font>');
		$.get("file_operation.php?action=get_files&id="+iid,
			function(data){
				$('#attachment').html(data);
			} 
		);
	}
);
var iid = '<? echo $iid; ?>';
function upload(){
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
		$('#uniqueId').hide();
	})
	.ajaxComplete(function(){
		$(this).hide();
		$('#uniqueId').show();
	});

	$.ajaxFileUpload({
		url:'file_operation.php?action=upload&id='+iid,
		secureuri:false,
		fileElementId:'uniqueId',
		dataType: 'json',
		success: function (data, status){
			if(typeof(data.error) != 'undefined'){
				if(data.error != ''){
					alert(data.error);
				}else{
					var fileTag = '<tr id="file'+ data.fileId + '"><td>'+data.link+'</td>';
					fileTag += '<td><a href="#" onclick="remove('+data.fileId+')">remove</a></td></tr>';
					//not work in IE6
					//tinyMCE.execCommand('mceInsertContent', false, '<a href="'+data.link+'">'+data.link+'</a>');
					$('#attachment table').append(fileTag);
					//some thing wrong with above line, so i refresh page instead . haha! idiot way.
					//window.location.href = window.location.href;
				}
			}
		},
		error: function (data, status, e){
			alert(e);
		}
	});
	return false;
}
function remove(fid){
	$.get("file_operation.php?action=unload&id="+fid, 
	  function(data){
		$('#status').text(data);
		$('#status').show();
		if(data.substring(0,5)!='error'){
			$('#file'+fid).hide();		
		}
	  });
}
function itemReq(){
	var br = '<br />';
	var err = '';
	if($('#title').val()==''){
		err += 'o  Title field requires'+br;
	}
	if(err != '')err = err.substring(0,err.length-br.length);
	return err;
}
</script>
<!-- TinyMCE -->
<script language="javascript" type="text/javascript" src="script/tiny_mce/tiny_mce.js"></script>
<script language="javascript" type="text/javascript">
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		plugins : "table,advimage,advlink,contextmenu,paste,fullscreen",
		theme_advanced_buttons1_add : "fontselect",
		theme_advanced_buttons2_add : "separator,forecolor,backcolor",
		theme_advanced_buttons2_add_before: "pastetext,pasteword,separator",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "advhr,separator,fullscreen",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		content_css : "example_word.css",
		external_link_list_url : "example_link_list.js",
		external_image_list_url : "example_image_list.js",
		media_external_list_url : "example_media_list.js",
		file_browser_callback : "fileBrowserCallBack",
		paste_use_dialog : false,
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		theme_advanced_link_targets : "_something=My somthing;_something2=My somthing2;_something3=My somthing3;",
		paste_auto_cleanup_on_paste : true,
		paste_convert_headers_to_strong : false,
		paste_strip_class_attributes : "all",
		paste_remove_spans : false,
		paste_remove_styles : false		
	});

	function fileBrowserCallBack(field_name, url, type, win) {
		// This is where you insert your custom filebrowser logic
		alert("Filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

		// Insert new URL, this would normaly be done in a popup
		win.document.forms[0].elements[field_name].value = "someurl.htm";
	}
</script>
<!-- /TinyMCE -->
</head>

<body>
<form>
	<div class="viewer-head">Edit item '<?php echo $item['title']; ?>' </div>
	<div class="input">
		<div class="title">Title *</div>
		<div><input style="font-size:large;width:100%" name="title" type="text" value='<?php echo $item['title']; ?>'></div>
	</div>
	<div class="input">
		<div class="title">Attachment</div>
		<div id="attachment"></div>
	</div>
	<div class="input">
		<div class="title">Content</div>
		<div>        
		<textarea id="elm1" name="description" rows="1" cols="80" style="width:50%">
			<?php echo $item['description']; ?>
		</textarea></div>
	</div>
	<input type="hidden" name="itemid" value="<?php echo $item['id']; ?>" />
	<div id="status"></div>
</form>
</body>
</html>

